package com.exam.service.impl;

import com.exam.dao.AnswerDao;
import com.exam.dao.ExamDao;
import com.exam.dao.QuestionDao;
import com.exam.dao.RecordDao;
import com.exam.domain.entity.Answer;
import com.exam.domain.entity.Exam;
import com.exam.domain.entity.Question;
import com.exam.domain.entity.Record;
import com.exam.service.RecordService;
import com.exam.util.BaseResult;

import java.util.List;

public class RecordServiceImpl implements RecordService {

    private final RecordDao recordDao;
    private final ExamDao examDao;
    private final QuestionDao questionDao;
    private final AnswerDao answerDao;
    public RecordServiceImpl() {
        recordDao = new RecordDao();
        examDao = new ExamDao();
        questionDao = new QuestionDao();
        answerDao = new AnswerDao();
    }

    @Override
    public BaseResult<List<Record>> getExamRecords(Integer studentId) {
        List<Record> records = recordDao.getExamRecordListByStudentId(studentId);
        for (Record record : records) {
            // 通过记录获取考试的详情
            Integer examId = record.getExamId();
            Exam exam = examDao.getExamInfoById(examId);
            record.setExam(exam);
        }
        return BaseResult.success(records);
    }

    @Override
    public BaseResult<Record> getExamRecordItemsById(Integer recordId) {
        Record record = recordDao.getExamRecordById(recordId);
        Integer examId = record.getExamId();
        // 通过试卷id，查询试卷信息
        Exam exam = examDao.getExamInfoById(examId);
        // 查询到试卷信息，再去查询，问题列表，以及学生的答题情况
        List<Question> questions = questionDao.getQuestionListByExamId(examId);
        for (Question question : questions) {
            Answer answer = answerDao.getAnswerByRecordId(recordId,question.getQuestionId());
            question.setAnswer(answer);
        }
        exam.setQuestions(questions);
        record.setExam(exam);
        return BaseResult.success(record);
    }
}
